package com.api.gbhg.daiyanlu.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.IndependentFinanceDao;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 非独立核算单位财务项目执行情况统计
 * Mr.dyl
 */
public class IndependentFinanceActionApi {
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    @POST
    @Path("/getFinance")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getFinance(Map<String,Object> maps){
        if (maps.containsKey("dw") && maps.get("dw") instanceof Boolean && !(Boolean)maps.get("dw")) {
            return JSON.toJSONString((Object)null);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

            Integer pageNum = 1;
            Integer pageSize = 5;
            String nd = sdf.format(new Date());
            String dw = "2";
            if (maps.containsKey("pageNum") && maps.get("pageNum") instanceof Integer && (Integer) maps.get("pageNum") > 0)
                pageNum = (Integer) maps.get("pageNum");
            if (maps.containsKey("pageSize") && maps.get("pageSize") instanceof Integer && (Integer) maps.get("pageSize") > 0)
                pageSize = (Integer) maps.get("pageSize");
            if (maps.containsKey("nd") && maps.get("nd") instanceof Integer)
                nd = maps.get("nd").toString();
            if (maps.containsKey("dw") && maps.get("dw") instanceof Integer) {
                dw = AlgorithmUtil.getSubCompanIdBybmId((Integer) maps.get("dw"));
            }


            RecordSet rs = IndependentFinanceDao.getMainTable(pageNum, pageSize, nd, dw, 0);
            Map<String, Object>[] map = new HashMap[rs.getArray().size()];
            int i = 0;
            while (rs.next()) {
                map[i] = new HashMap<String, Object>();
                map[i].put("xmmc", rs.getString("xmmc"));
                RecordSet sbdwRs = rmcd.getSBDWById(rs.getInt("sbdw"));
                sbdwRs.next();
                map[i].put("ssdw", sbdwRs.getString("SUBCOMPANYNAME"));
                map[i].put("ysje", rs.getString("ze"));
                map[i].put("zxl", AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("ze")));
                i++;
            }
            com.api.gbhg.zengping.page.PageInfo<String> pageInfo = new com.api.gbhg.zengping.page.PageInfo<>();
            int cut = IndependentFinanceDao.getMainTableCount(nd, dw, 0);
            pageInfo.setPageNum(pageNum);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTotal(cut);
            pageInfo.setList(map);
            return JSON.toJSONString(pageInfo);
        }
    }
}
